package com.htc.callcare.activities;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.concurrent.ExecutionException;

import com.htc.callcare.R;
import com.htc.callcare.adapter.VoiceMailAdapter;
import com.htc.callcare.asyntask.AsynLoadAllVoiceMailFile;
import com.htc.callcare.database.Database;
import com.htc.callcare.datacontroller.VoiceMailFileController;
import com.htc.callcare.model.VoiceMailModel;
import com.htc.callcare.viewcontroller.Player;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class VoiceMailActivity extends Activity {
	Database database;
	TextView txtNoVoiceMail;
	ListView listVoiceMail;
	Player player;
	LinearLayout playLayout;
	ArrayList<VoiceMailModel> voiceMails;
	Hashtable hashtable = new Hashtable();
	VoiceMailModel processVoiceMail;
	String selectedContactName;
	VoiceMailFileController voiceMailFileController;
	VoiceMailAdapter adapter;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.record_screen);
		initView();

	}

	private void initView() {
		// TODO Auto-generated method stub
		voiceMailFileController = new VoiceMailFileController(this);
		database = new Database(this);
		txtNoVoiceMail = (TextView) findViewById(R.id.noRecordFile);
		listVoiceMail = (ListView) findViewById(R.id.listRecord);
		registerForContextMenu(listVoiceMail);
		// Init Player
		player = new Player(this);
		playLayout = (LinearLayout) findViewById(R.id.layoutPlay);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		playLayout.addView(player, params);
		player.hidePlayer();

	}

	public void onResume() {
		super.onResume();
		reFreshAdapter();
		Log.e("thuutai", "Count voice mail check"+database.countAllVocieMailCheck());
	}

	protected void onDestroy() {
		// TODO Auto-generated method stub
		player.release();
		super.onDestroy();
	}

	protected void onPause() {
		// TODO Auto-generated method stub
		player.stop();
		super.onPause();
	}

	public void reFreshAdapter() {
		AsynLoadAllVoiceMailFile allVoiceMailFile = new AsynLoadAllVoiceMailFile(
				this, "");
		allVoiceMailFile.execute();
		try {
			voiceMails = allVoiceMailFile.get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (voiceMails.size() != 0) {
			Toast.makeText(this, "" + voiceMails.size(), Toast.LENGTH_SHORT)
					.show();
			adapter = new VoiceMailAdapter(this, voiceMails,
					getAllContactName());
			listVoiceMail.setAdapter(adapter);
		} else {
			txtNoVoiceMail.setText(this.getString(R.string.noVoiceMailFile));
			txtNoVoiceMail.setVisibility(View.VISIBLE);
		}
	}

	public Hashtable getAllContactName() {

		Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
		String[] projection = new String[] {
				ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
				ContactsContract.CommonDataKinds.Phone.NUMBER };

		Cursor people = getContentResolver().query(uri, projection, null, null,
				null);

		int indexName = people
				.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
		int indexNumber = people
				.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

		people.moveToFirst();
		do {
			String name = people.getString(indexName);
			String number = people.getString(indexNumber);
			hashtable.put(number, name);
			// Do work...
		} while (people.moveToNext());
		return hashtable;
	}

	public void showPlayerView() {
		player.setVisibility(View.VISIBLE);
	}

	public void setVoiceMailFile(String fileUri) {
		if (voiceMails.size() > 0) {
			player.setFilename(fileUri);
			player.setPlayer();
			Toast.makeText(getApplicationContext(), "Play", Toast.LENGTH_SHORT)
					.show();
		}
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.voice_mail_context_menu, menu);
	}
	
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.voice_mail_call:
			Intent callIntent = new Intent(Intent.ACTION_CALL);
			callIntent.setData(Uri.parse("tel:"+processVoiceMail.getCallNumber()));
			startActivity(callIntent);
			return true;
		case R.id.voice_mail_share:
			voiceMailFileController.send(processVoiceMail);
			return true;
		case R.id.voice_mail_delete:
			voiceMailFileController.delete(processVoiceMail, this, adapter);
			return true;

		default:
			return super.onContextItemSelected(item);
		}
	}

	public void setSelectedVoiceMail(VoiceMailModel model, String contact_name) {
		this.processVoiceMail = model;
		this.selectedContactName = contact_name;
	}

}
